"""
NC97 字符串出现次数的TopK问题
https://www.nowcoder.com/practice/fd711bdfa0e840b381d7e1b82183b3ee?tpId=117&&tqId=37809&&companyId=239&rp=1&ru=/company/home/code/239&qru=/ta/job-code-high/question-ranking
"""

#
# 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
#
# return topK string
# @param strings string字符串一维数组 strings
# @param k int整型 the k
# @return string字符串二维数组
#

from typing import List
from collections import Counter


class Solution:
    def method1(self, strings: List[str], k: int) -> List[List[str]]:
        if not strings:
            return []

        # 计数,也可以自定义map统计
        c = Counter(strings)

        # 这里不用这个c.most_common(k)，是因为次数相同时，无法保证字典序
        # c.most_common(k)
        lst = sorted(c.items(), key=lambda item: (-item[1], item[0]))
        res = [[k, v] for k, v in lst[:k]]
        return res

    def topKstrings(self, strings: List[str], k: int) -> List[List[str]]:
        # write code here
        return self.method1(strings, k)


def test():
    obj = Solution()
    strings, k = ["a", "b", "c", "b"], 2
    res = obj.topKstrings(strings, k)
    print(res)


if __name__ == "__main__":
    test()
